﻿using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace YaP.Library.Extensions
{
    public static class StringTools
    {
        /// <summary>
        /// Merge an Array of string into a single string.
        /// </summary>
        /// <param name="TabString">Array of String</param>
        /// <param name="separator">Separator</param>
        /// <returns></returns>
        public static string Merge(this string[] TabString, char separator)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < TabString.Length; i++)
            {
                sb.Append(TabString[i] + separator);
            }

            return sb.ToString();
        }

        /// <summary>
        /// Merge an Array of string into a single string.
        /// </summary>
        /// <param name="TabString">Array of String</param>
        /// <param name="separator">Separator</param>
        /// <returns></returns>
        public static string Merge(this string[] TabString, string separator)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < TabString.Length; i++)
            {
                sb.Append(TabString[i] + separator);
            }

            return sb.ToString();
        }
        public static  string Merge(this string[] TabString, string separator, int start)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = start; i < TabString.Length; i++)
            {
                sb.Append(TabString[i] + separator);
            }

            return sb.ToString();

        }
        public static bool ContainsOnlySpaces(this string str)
        {
            Regex expr = new Regex(@"^(\s+)$");
            return expr.IsMatch(str);
        }

        public static string[] DeleteEmptyCells(this string[] ArrayOfString)
        {
            ArrayList ToReturn = new ArrayList();
            foreach (string cell in ArrayOfString)
            {
                if (cell != string.Empty)
                {
                    ToReturn.Add(cell);
                }
            }
            return ToReturn.ToArray(typeof (string)) as string[];
        }

        public static int CountChars(this string obj, char value)
        {
            int Total = 0;
            for (int i = 0; i < obj.Length; i++)
            {
                if (obj[i] == value)
                {
                    Total++;
                }
            }
            return Total;
        }
    }
}